********************************
** Read in file, add controls **

******************************
** SET UP DEFAULT FILE (Baseline Specification) **
use 	"./output/cities_expandedpatents", clear

merge m:1 npi_id using "./output/intermediate/controls_43cities"
drop _merge

merge m:1 extra_id using "./output/intermediate/controls_7cities", update
drop _merge

** npi X month-of-year FEs **
egen	idxm = group(gen_id f_m)

** census region X year FEs **
egen	regyr = group(f_yr region)

gen		daysnpis = days_npis/30 
gen		lnnpis = ln(days_npis)
gen		longnpis = (days_npis>=90) if !mi(days_npis) // Natural break in data, about 61% have short
gen		ldeaths = ln(mag_maxexcessdeaths)

foreach v of varlist daysnpis longnpis lnnpis ldeaths {
	gen 	post_X_`v' 		= `v'*(f_myr>=tm(1918m9))

	gen 	during_X_`v' 	= `v'*( (f_myr>=tm(1918m9)) & (f_myr<=tm(1919m3)) )
	gen 	after_X_`v' 	= `v'*(f_myr>=tm(1919m4))
	gen 	pre_X_`v' 		= `v'*(f_myr<=tm(1917m8))
	
	lab var post_X_`v'		"Post Pandemic $\times$ NPI Length"
	
	lab var pre_X_`v'		"Before Pandemic $\times$ NPI Length"
	lab var during_X_`v' 	"During Pandemic $\times$ NPI Length"
	lab var after_X_`v' 	"After Pandemic $\times$ NPI Length"
}

drop 	daysnpis 

******************************
** SET UP PATCLASS Files **

frame create patclass1d 
frame create patclass3d 

foreach n of numlist 1 3 {

	frame change patclass`n'd

	use 	"./output/cities_expandedpatents_longclass`n'd", clear

	** city X month-of-year FEs **
	egen	idxm = group(gen_id f_m)
	** census region X year FEs **
	egen	regyr = group(f_yr region)
	** city X class FEs
	egen 	idxp = group(gen_id class_`n'd)
	** class X month-and-year FEs
	egen 	pxfmyr = group(f_myr class_`n'd)
	** city X class X month-of-year FEs 
	egen	idxmxp = group(gen_id f_m class_`n'd)
	** class x census region X year FEs ** 
	egen	 pxregyr = group(f_yr region class_`n'd)

	compress

	*=======================*
	*= Treatment variables  *
	*=======================*

	tab 	days_npis if f_myr==tm(1918m9)

	gen		daysnpis = days_npis/30 
	gen		lnnpis = ln(days_npis)
	gen		longnpis = (days_npis>=90) if !mi(days_npis) // Natural break in data, about 61% have short
	gen		ldeaths = ln(mag_maxexcessdeaths)

	foreach v of varlist daysnpis longnpis lnnpis ldeaths {
		gen 	post_X_`v' 		= `v'*(f_myr>=tm(1918m9))

		gen 	during_X_`v' 	= `v'*( (f_myr>=tm(1918m9)) & (f_myr<=tm(1919m3)) )
		gen 	after_X_`v' 	= `v'*(f_myr>=tm(1919m4))
		gen 	pre_X_`v' 		= `v'*(f_myr<=tm(1917m8))
		
		lab var post_X_`v'		"Post Pandemic $\times$ NPI Length"
		
		lab var pre_X_`v'		"Before Pandemic $\times$ NPI Length"
		lab var during_X_`v' 	"During Pandemic $\times$ NPI Length"
		lab var after_X_`v' 	"After Pandemic $\times$ NPI Length"
	}

	drop 	daysnpis 
}

frame change default

*********************
** Run Estimation Across Frames **

est clear

** Table 1 Panel A
foreach v in daysnpis longnpis {
	
	local treat post_X_`v'

	eststo: ppmlhdfe pat_wtd_inv `treat' if tin(1916m1, 1920m12), exp(pop_int) a(f_myr gen_id) cluster(gen_id)
	eststo: ppmlhdfe pat_wtd_inv `treat' if tin(1916m1, 1920m12), exp(pop_int) a(f_myr idxm) cluster(gen_id)
	eststo: ppmlhdfe pat_wtd_inv `treat' if tin(1916m1, 1920m12), exp(pop_int) a(f_myr idxm regyr) cluster(gen_id)
	eststo: ppmlhdfe pat_wtd_inv `treat' if tin(1910m1, 1926m12), exp(pop_int) a(f_myr idxm regyr) cluster(gen_id)
	
	frame patclass1d: ppmlhdfe pat_wtd_inv `treat' if tin(1916m1, 1920m12), exp(pop_int) a(idxmxp pxfmyr pxregyr) cluster(gen_id)
	eststo est5
	
	frame patclass3d: ppmlhdfe pat_wtd_inv `treat' if tin(1916m1, 1920m12), exp(pop_int) a(idxmxp pxfmyr pxregyr) cluster(gen_id)
	eststo est6
	
	estout 	using "$RES/fortable/main_posteffect_`v'.tex", replace cells(b(star fmt(%9.3f)) se(par)) ///
		stats(N, fmt(%9.0g) labels("\(N\)"))  starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
		style(tex) drop(_cons) eqlabels(none) label
	est 	clear
}

** Table 1 Panel A
foreach v in daysnpis longnpis  {
	
	local treat pre_X_`v' during_X_`v' after_X_`v'

	eststo: ppmlhdfe pat_wtd_inv `treat' if tin(1916m1, 1920m12), exp(pop_int) a(f_myr gen_id) cluster(gen_id)
	eststo: ppmlhdfe pat_wtd_inv `treat' if tin(1916m1, 1920m12), exp(pop_int) a(f_myr idxm) cluster(gen_id)
	eststo: ppmlhdfe pat_wtd_inv `treat' if tin(1916m1, 1920m12), exp(pop_int) a(f_myr idxm regyr) cluster(gen_id)
	eststo: ppmlhdfe pat_wtd_inv `treat' if tin(1910m1, 1926m12), exp(pop_int) a(f_myr idxm regyr) cluster(gen_id)

	frame patclass1d: ppmlhdfe pat_wtd_inv `treat' if tin(1916m1, 1920m12), exp(pop_int) a(idxmxp pxfmyr pxregyr) cluster(gen_id)
	eststo
	
	frame patclass3d: ppmlhdfe pat_wtd_inv `treat' if tin(1916m1, 1920m12), exp(pop_int) a(idxmxp pxfmyr pxregyr) cluster(gen_id)
	eststo
	
	estout 	using "$RES/fortable/main_duringafterpre_`v'.tex", replace cells(b(star fmt(%9.3f)) se(par)) ///
		stats(N, fmt(%9.0g) labels("\(N\)"))  starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
		style(tex) drop(_cons) eqlabels(none) label
	est 	clear
}

clear frames
